﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace AnonsaiWPF.CSharp
{
    public class TitleControl : System.Windows.Controls.Control
    {
        CloseButton closeButton;
  
        public TitleControl()
        {
            this.MouseLeftButtonDown += new MouseButtonEventHandler(OnTitleBarLeftButtonDown);
            this.Loaded += new RoutedEventHandler(TitleBar_Loaded);
        }


        void TitleBar_Loaded(object sender, RoutedEventArgs e)
        {
            closeButton = (CloseButton)this.Template.FindName("CloseButton", this);

            closeButton.Click += new RoutedEventHandler(CloseButton_Click);
         }


        static TitleControl()
        {
           DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleControl), new FrameworkPropertyMetadata(typeof(TitleControl)));
        }

        #region event handlers

        void OnTitleBarLeftButtonDown(object sender, MouseEventArgs e)
        {
            Window window = this.TemplatedParent as Window;
            if (window != null)
            {
                window.DragMove();
            }
        }

        void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Window window = this.TemplatedParent as Window;
            if (window != null)
            {
                window.Close();
            }
        }

        #endregion

        #region properties

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }


        #endregion

        #region dependency properties

        public static readonly DependencyProperty TitleProperty =
           DependencyProperty.Register(
               "Title", typeof(string), typeof(TitleControl));

        public static readonly DependencyProperty IconProperty =
           DependencyProperty.Register(
               "Icon", typeof(ImageSource), typeof(TitleControl));

        #endregion
    }
}
